/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-29
*/
package com.rzico.order.entity;

import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.order.model.CartItemVo;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * <pre>
 *   购物车数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "wx_cart")
public class Cart{

    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "key" )
    protected String cartKey;

    @ApiModelProperty(value = "会员" )
    protected Long memberId;

    @JsonIgnore
    @IgnoreSwaggerParameter
    public List<CartItem> itemList = Collections.emptyList();

    @ApiModelProperty(value = "购物车列表" )
    public List<CartItemVo> itemVoList = Collections.emptyList();

    public CartItem findItem(Long productId) {
        for (CartItem cartItem:itemList) {
            if (cartItem.getProductId().equals(productId)) {
                return cartItem;
            }
        }
        return null;
    }

    public Integer getQuantity() {
        Integer qry = 0;
        for (CartItem cartItem:itemList) {
            if (cartItem.getChk()) {
                qry = qry + cartItem.getQuantity();
            }
        }
        return qry;
    }


    public BigDecimal getAmount() {
        BigDecimal amount = BigDecimal.ZERO;
        for (CartItem cartItem:itemList) {
            if (cartItem.getChk()) {
                amount = amount.add(cartItem.getPrice().multiply(new BigDecimal(cartItem.getQuantity())));
            }
        }
        return amount;
    }

}
